﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Usercontrol for selectiong a single role
/// </summary>
public partial class SingleRoleSelection : ObjectSelection
{

    /// <summary>
    /// Gets the selected role.
    /// </summary>
    /// <value>The selected role.</value>
    public Role SelectedRole
    {
        get
        {
            if (String.IsNullOrEmpty(DropDownListRoles.SelectedValue))
            {
                return null;
            }
            return DAORole.GetRoleByID(Convert.ToInt32(DropDownListRoles.SelectedValue));
        }
    }

    /// <summary>
    /// Holds selected role ID
    /// </summary>
    public int SelectedRoleID
    {
        get
        {
            if (String.IsNullOrEmpty(DropDownListRoles.SelectedValue))
            {
                return -1;
            }
            return Convert.ToInt32(DropDownListRoles.SelectedValue);
        }        
        set
        {
            ListItem li = DropDownListRoles.Items.FindByValue(value.ToString());
            if (li != null)
            {
                li.Selected = true;
            }
        }
    }

    public void ResetRole()
    {
        DropDownListRoles.SelectedIndex = 0;
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind();
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void DataBind()
    {
        base.DataBind();
        DropDownListRoles.DataSource = DAORole.GetAllRoles(false);
        DropDownListRoles.DataTextField = "Name";
        DropDownListRoles.DataValueField = "ID";
        DropDownListRoles.DataBind();
        if (this.IsFilter)
        {
            DropDownListRoles.Items.Insert(0, TopListItemForFilter(ObjectSelectionType.Role.ToString()));
        }
    }

    /// <summary>
    /// Clears the roles.
    /// </summary>
    public void Clear()
    {
        DropDownListRoles.SelectedIndex = 0;
    }

    protected override Label GetLabel()
    {
        return this.LabelRoles;
    }

    protected override WebControl GetWebControl()
    {
        return this.DropDownListRoles;
    }
}
